//
//  XLMineSwitchTableViewCell.m
//  Mine
//
//  Created by 于鹏 on 2017/8/30.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "XLMineSwitchTableViewCell.h"
#import "XLMineSwitchCellModel.h"
//#import <Masonry.h>
#import "Masonry.h"

@interface XLMineSwitchTableViewCell()
@property (nonatomic, strong)UISwitch *switchItem;  ///<开关

@end

@implementation XLMineSwitchTableViewCell

+ (XLBaseTableViewCell *)cellWithIdentifier:(NSString *)cellIdentifier tableView:(UITableView *)tableView {
    XLMineSwitchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[XLMineSwitchTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    return cell;
}

- (void)setupUI {
    [super setupUI];
    //添加开关控件
    UISwitch *switchItem = [[UISwitch alloc] initWithFrame:CGRectZero];
    [switchItem addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    [self.contentView addSubview:switchItem];
    self.switchItem = switchItem;
    
    [self.switchItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-XL_KCellMargin);
        make.centerY.equalTo(self.contentView.mas_centerY).offset(0);
        make.size.mas_equalTo(CGSizeMake(XL_KSwitchWidth, XL_KSwitchHeight));
    }];
}

- (void)setupDataModel:(XLBaseCellModel *)model {
    [super setupDataModel:model];
    XLMineSwitchCellModel *switchModel = (XLMineSwitchCellModel *)model;
    self.switchItem.on = switchModel.on;
}

- (void)switchChanged:(UISwitch *)switchItem {
    XLMineSwitchCellModel *model =
    (XLMineSwitchCellModel *)self.cellModel;
    model.on = switchItem.on;
    if(model.switchBlock){
        model.switchBlock((XLMineBaseCellModel *)self.cellModel,self.indexPath,switchItem.on);
    }

}


@end
